// my_dian/caipgl/caipgl.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: '',
    title: "菜品管理",
    plgl: false,
    cai: [],
    currindex: 0,
    capin: [],
    caid: [],
    page: 1,
    cid: '',
    fid: 0,
    keyWord: '', //关键字
  },
  // 搜索关键字
  keyWord(e) {
    this.setData({
      keyWord: e.detail.value
    })
    if (this.data.keyWord == '') {
      this.zuolist()
    } else {
      var that = this
      wx.showLoading({
        title: '加载中',
        icon: 'none'
      })
      this.myRequest(yuming + 'hallfood/goodsKeywordsGoods', {
        shopId: wx.getStorageSync('shopId'),
        keyWords: e.detail.value
      }, 'POST').then(res => {
        wx.hideLoading();
        that.setData({
          capin: res.data.data
        })
      })
    }
  },
  // 右边列表
  youlist(cid) {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'hallfood/goodsSelectGoods', {
      shopId: wx.getStorageSync('shopId'),
      cid: cid,
      page: that.data.page
    }, 'POST').then(res => {
      wx.hideLoading();
      that.setData({
        capin: res.data.data
      })
    })
  },
  // 左边列表
  zuolist(e) {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'hallfood/goodsSelectClass', {
      shopId: wx.getStorageSync('shopId'),
      fid: that.data.fid
    }, 'POST').then(res => {
      wx.hideLoading();
      that.setData({
        cai: res.data.data.result,
        cid: res.data.data.result[0].id
      })
      that.youlist(res.data.data.result[0].id)
    })
  },
  // 右边多选
  caixuan(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var indexs = e.currentTarget.dataset.indexs;
    var item = this.data.capin
    if (!this.data.plgl) {
      wx.navigateTo({
        url: '/my_dian/addcai/addcai?title=修改菜品&state=1&id=' + id,
      })
    } else {
      item[index].goodsList[indexs].check = !item[index].goodsList[indexs].check
      this.setData({
        capin: item
      })
      var arr = []
      for (var i = 0; i < item.length; i++) {
        for (var k = 0; k < item[i].goodsList.length; k++) {
          if (item[i].goodsList[k].check) {
            arr.push(item[i].goodsList[k].id)
          }
        }
      }
      this.setData({
        caid: arr
      })
    }
  },
  // 切换菜
  caiqie(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    this.setData({
      currindex: index,
      cid: id
    })
    this.youlist(id)
  },
  // 批量管理
  piliang() {
    var item = this.data.capin
    for (var i = 0; i < item.length; i++) {
      for (var k = 0; k < item[i].goodsList.length; k++) {
        item[i].goodsList[k].check = false
      }
    }
    this.setData({
      plgl: !this.data.plgl,
      caid: [],
      capin: this.data.capin
    })
  },
  // 删除
  shanchu() {
    if (this.data.caid.length == 0) {
      wx.showToast({
        title: '请选择要删除的菜品',
        icon: 'none'
      })
    } else {
      var that = this
      wx.showModal({
        title: '您确定要删除吗？',
        cancelText: '取消',
        confirmText: '确定',
        success: function (res) {
          if (res.confirm) {
            console.log("确定")
            that.myRequest(yuming + 'hallfood/goodsDelGoods', {
              shopId: wx.getStorageSync('shopId'),
              ids: that.data.caid,
            }, 'POST').then(res => {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
              if (res.data.code == 200) {
                setTimeout(function () {
                  that.zuolist()
                  that.youlist()
                }, 1000)
              }
            })
          } else {
            console.log("取消")
          }
        }
      })
    }
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 添加菜品
  addcai() {
    wx.navigateTo({
      url: '/my_dian/addcai/addcai?title=添加菜品&state=0',
    })
    this.setData({
      plgl: false
    })
  },
  // 分类管理
  fenl() {
    wx.navigateTo({
      url: '/my_dian/fengl/fengl',
    })
    this.setData({
      plgl: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      currindex: 0
    })
    if (this.data.keyWord == '') {
      this.zuolist()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})